﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;

namespace desarrolloMobile.DGTViewer
{
    public partial class Settings : PhoneApplicationPage
    {
        public Settings()
        {
            InitializeComponent();
            
            var context = (from incidencia in App.ViewModel.Incidencias
                          select incidencia.Poblacion)
                          .Distinct()
                          .OrderBy(incidencia => incidencia);

            context.ToList().ForEach(
                poblacion =>
                this.lstBoxPoblacion.Items.Add(
                    new TextBlock()
                        {
                            Text = poblacion,
                            FontSize = 40,
                            Style = (Style)Application.Current.Resources["PhoneTextGroupHeaderStyle"]
                        }));

            context = (from incidencia in App.ViewModel.Incidencias
                           select incidencia.Autonomia)
                                .Distinct()
                                .OrderBy(incidencia => incidencia);

            context.ToList().ForEach(
                autonomia =>
                this.lstBoxAutonomia.Items.Add(
                    new TextBlock
                    {
                        Text = autonomia,
                        FontSize = 40,
                        Style = (Style)Application.Current.Resources["PhoneTextGroupHeaderStyle"]
                    }
                    ));

            context = (from incidencia in App.ViewModel.Incidencias
                       select incidencia.Carretera)
                    .Distinct()
                    .OrderBy(incidencia => incidencia);

            context.ToList().ForEach(
                carretera =>
                this.lstBoxCarretera.Items.Add(
                    new TextBlock
                    {
                        Text = carretera,
                        FontSize = 40,
                        Style = (Style)Application.Current.Resources["PhoneTextGroupHeaderStyle"]
                    }
                    ));

            App.ViewModel.
                IsDataLoaded = false;   
        }

        private void lstBoxAutonomia_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((TextBlock) lstBoxAutonomia.SelectedItem).FontSize -= 6;
            App.ViewModel.PredicadoValor = ((TextBlock) this.lstBoxAutonomia.SelectedItem).Text;
            App.ViewModel.PredicadoCampo = "Autonomia";
            App.ViewModel.Predicado = (incidencia => incidencia.Autonomia == App.ViewModel.PredicadoValor);
            NavigationService.GoBack();
        }

        private void lstBoxPoblacion_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((TextBlock)lstBoxPoblacion.SelectedItem).FontSize -= 6;
            App.ViewModel.PredicadoValor = ((TextBlock)this.lstBoxPoblacion.SelectedItem).Text;
            App.ViewModel.PredicadoCampo = "Poblacion";
            App.ViewModel.Predicado = (incidencia => incidencia.Poblacion == App.ViewModel.PredicadoValor);
            NavigationService.GoBack();
        }

        private void lstBoxCarretera_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((TextBlock)lstBoxCarretera.SelectedItem).FontSize -= 6;
            App.ViewModel.PredicadoValor = ((TextBlock)this.lstBoxCarretera.SelectedItem).Text;
            App.ViewModel.PredicadoCampo = "Carretera";
            App.ViewModel.Predicado = (incidencia => incidencia.Carretera == App.ViewModel.PredicadoValor);
            NavigationService.GoBack();
        }

        private void optTodas_Checked(object sender, RoutedEventArgs e)
        {
            App.ViewModel.PredicadoValor = string.Empty;
            App.ViewModel.PredicadoCampo = string.Empty;
            App.ViewModel.Predicado = (i => true);
            NavigationService.GoBack();
        }

        private void optHoy_Checked(object sender, RoutedEventArgs e)
        {
            App.ViewModel.PredicadoValor = "24";
            App.ViewModel.PredicadoCampo = "Fecha";
            App.ViewModel.Predicado = 
                (incidencia => 
                    DateTime.Parse(incidencia.FechaHora) >= 
                    DateTime.Today.Subtract(new TimeSpan(1,0,0,0)));
            NavigationService.GoBack();
        }

        private void optUltimaHora_Checked(object sender, RoutedEventArgs e)
        {
            App.ViewModel.PredicadoValor = "1";
            App.ViewModel.PredicadoCampo = "Hora";
            App.ViewModel.Predicado = 
                (incidencia => 
                    DateTime.Parse(incidencia.FechaHora) >= 
                    DateTime.Now.Subtract( new TimeSpan(1,0,0)));
            NavigationService.GoBack();
        }

    }
}